package com.cade.agriculturemall.view.classify;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.cade.agriculturemall.model.ItemModelOfDrawerListEntity;
import com.cade.agriculturemall.R;
import com.cade.agriculturemall.base.BaseFragment;
import com.cade.agriculturemall.network.NetBroadcastReceiver;
import com.cade.agriculturemall.utils.DrawerListAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lobby on 2017/11/16.
 */

public class ClassifyFragment extends BaseFragment<ClassifyContract.IPresenter> implements ClassifyContract.IView {
    private LinearLayout mLLContent;
    private DrawerLayout mDrawerLayout;
    private LinearLayout mLLDrawer;
    private Toolbar mToolbar;
    private ListView mListViewDrawer;
    private List<ItemModelOfDrawerListEntity> items = new ArrayList<>();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int setupLayout() {
        return R.layout.layout_classify_fragment;

    }

    @Override
    public void setupView() {
        mLLDrawer = (LinearLayout) getView().findViewById(R.id.classify_fragment_ll_drawer);
        mListViewDrawer = (ListView) getView().findViewById(R.id.classify_fragment_lv_drawer);
        mToolbar = (Toolbar) getView().findViewById(R.id.toolbar);

        mDrawerLayout = (DrawerLayout) getView().findViewById(R.id.classify_fragment_drawer_layout);
        ClassifyNewFrameLayout fragment = (ClassifyNewFrameLayout) getView().findViewById(R.id.classify_fragment);
        fragment.setChildFragmentManager(getChildFragmentManager());
        fragment.getGoodsItemList();
    }

    //    抽屉内容
    @Override
    public void setupEvent() {
//        DrawerListAdapter mAdapter=new DrawerListAdapter(getActivity(),items);
//        mListViewDrawer.setAdapter(mAdapter);
        ActionBarDrawerToggle mToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, mToolbar, R.string.drawer_open, R.string.drawer_close);
        mToggle.syncState();
        mDrawerLayout.addDrawerListener(mToggle);
        mListViewDrawer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ItemModelOfDrawerListEntity item = items.get(position);
                Toast.makeText(getActivity(), "点击了 " + position + " 项", Toast.LENGTH_SHORT).show();
            }
        });
    }


    @Override
    public void setupData() {

    }

    //    加载抽屉数据
    @Override
    public void onLoadMenuComplete(List<ItemModelOfDrawerListEntity> itemModelOfDrawerListEntities) {
        items.clear();
        if (itemModelOfDrawerListEntities.size() != 0) {
            for (ItemModelOfDrawerListEntity model : itemModelOfDrawerListEntities) {
                model.tab = model.getVariety();
            }
        }
        items.addAll(itemModelOfDrawerListEntities);
        DrawerListAdapter mAdapter = new DrawerListAdapter(getActivity(), items);
        mListViewDrawer.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();

    }

    @Override
    public ClassifyContract.IPresenter setupPresenter() {
        return new ClassifyPresenter(this);
    }

    public void setItems(List<ItemModelOfDrawerListEntity> items) {
        this.items = items;
    }
}
